package com.peanut.devlibrary.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.peanut.baby.R;
import com.peanut.baby.widget.FloatMenuTextView;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;

public class FloatMenuMgr implements View.OnClickListener {
    public static final int MENU_RECORD = 0;
    public static final int MENU_BBS = 1;
    public static final int MENU_ARTICLE = 2;
    public static final int MENU_ASK = 3;
    public static final int MENU_SEARCH = 4;
    private Context mContext;

    FrameLayout parent;
    FloatMenuListener listener;

    FloatMenuTextView record;
    FloatMenuTextView bbs;
    FloatMenuTextView article;
    FloatMenuTextView ask;
    FloatMenuTextView search;

    private ArrayList<FloatMenuTextView> menus = new ArrayList<>();

    ImageView   toggle;
    boolean open = false;

    public void init(FrameLayout parent,FloatMenuListener listener){
        this.parent = parent;
        this.listener = listener;
        mContext = this.parent.getContext();
        initChildViews();
    }
    private void initChildViews(){
        record = parent.findViewById(R.id.menu_record);
        bbs = parent.findViewById(R.id.menu_moment);
        article = parent.findViewById(R.id.menu_article);
        ask = parent.findViewById(R.id.menu_ask);
        search = parent.findViewById(R.id.menu_search);
        toggle = parent.findViewById(R.id.menu_toggle);

        menus.add(record);
        menus.add(bbs);
        menus.add(article);
        menus.add(ask);
        menus.add(search);

        record.setOnClickListener(this);
        bbs.setOnClickListener(this);
        article.setOnClickListener(this);
        ask.setOnClickListener(this);
        search.setOnClickListener(this);
        toggle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menu_record:
                performClick(MENU_RECORD);
                break;
            case R.id.menu_moment:
                performClick(MENU_BBS);
                break;
            case R.id.menu_article:
                performClick(MENU_ARTICLE);
                break;
            case R.id.menu_ask:
                performClick(MENU_ASK);
                break;
            case R.id.menu_search:
                performClick(MENU_SEARCH);
                break;
            case R.id.menu_toggle:
                toggleMenu();
                break;
        }
    }
    private void toggleMenu(){
        if(open){
            closeMenu();
        }else{
            openMenu();
        }
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void openMenu(){
        for (int i = 0; i < menus.size(); i++) {
            View menu = menus.get(i);
            menu.setVisibility(View.VISIBLE);
            AnimatorSet set = new AnimatorSet();
            double dx = 0;
            double dy = 0;
            if(i == 0){
                dx = -dip2px(mContext,75);
                dy = dip2px(mContext,30);
            }else if(i == 1){
                dx = -dip2px(mContext,70);
                dy = -dip2px(mContext,15);
            }else if(i == 2){
                dx = -dip2px(mContext,50);
                dy = -dip2px(mContext,50);
            }else if(i == 3){
                dx = -dip2px(mContext,15);
                dy = -dip2px(mContext,70);
            }else if(i == 4){
                dx = dip2px(mContext,30);
                dy = -dip2px(mContext,80);
            }

            set.playTogether(
                    ObjectAnimator.ofFloat(menu,"translationX",(float)0,(float)dx),
                    ObjectAnimator.ofFloat(menu,"translationY",(float)0,(float)dy)
                    ,ObjectAnimator.ofFloat(menu,"alpha",0,1).setDuration(300)
            );
            set.setInterpolator(new OvershootInterpolator());
            set.setDuration(300).setStartDelay(100*i);
            set.start();

        }
        //转动加号大图标本身45°
        ObjectAnimator rotate = ObjectAnimator.ofFloat(toggle,"rotation",0,45).setDuration(300);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.start();
        open = true;

    }

    public void closeMenu(){
        if(!open){
            return;
        }

        for (int i = 0; i < menus.size(); i++) {
            View menu = menus.get(i);

            AnimatorSet set = new AnimatorSet();
            double dx = 0;
            double dy = 0;
            if(i == 0){
                dx = -dip2px(mContext,75);
                dy = dip2px(mContext,30);
            }else if(i == 1){
                dx = -dip2px(mContext,70);
                dy = -dip2px(mContext,15);
            }
            else if(i == 2){
                dx = -dip2px(mContext,50);
                dy = -dip2px(mContext,50);
            }else if(i == 3){
                dx = -dip2px(mContext,15);
                dy = -dip2px(mContext,70);
            }else if(i == 4){
                dx = dip2px(mContext,30);
                dy = -dip2px(mContext,80);
            }


            set.playTogether(
                    ObjectAnimator.ofFloat(menu,"translationX",(float)dx,(float)0),
                    ObjectAnimator.ofFloat(menu,"translationY",(float)dy,(float)0)
                    ,ObjectAnimator.ofFloat(menu,"alpha",1,0).setDuration(300)
            );
            set.setInterpolator(new LinearInterpolator());
            set.setDuration(300).setStartDelay(100*i);
            set.start();
//            menu.setVisibility(View.GONE);

        }

        //转动加号大图标本身45°
        ObjectAnimator rotate = ObjectAnimator.ofFloat(toggle,"rotation",45,0).setDuration(300);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.start();
        open = false;
    }

    private void performClick(int type){
        Log.d("FloatMenuMgr","performClick "+type);
        if(listener != null){
            closeMenu();
            listener.onMenuClicked(type);
        }
    }


}

